package com.zlc.platform.controller;

import com.zlc.common.annotation.LogSaveAnnotation;
import com.zlc.common.common.constant.PlatConstant;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.core.role.model.entity.MayanPlatRole;
import com.zlc.common.core.role.model.vo.MayanPlatDataRoleConfigVo;
import com.zlc.common.core.role.model.vo.MayanPlatRoleClassVo;
import com.zlc.common.core.role.model.vo.MayanPlatRoleUserVo;
import com.zlc.common.core.role.model.vo.MayanPlatRoleVo;
import com.zlc.common.core.user.model.vo.MayanPlatUserVo;
import com.zlc.common.model.resource.ResourceEntity;
import com.zlc.common.model.resource.ResourceRoleVo;
import com.zlc.common.model.role.RolePageRequest;
import com.zlc.common.model.role.RoleUserPageRequest;
import com.zlc.platform.model.RoleResourceEntity;
import com.zlc.platform.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色相关")
@RestController
@Slf4j
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加角色类别
     */
    @ApiOperation(value = "添加角色类别")
    @PostMapping(value = "/addRoleClass")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "添加角色类别", catalog = "权限管理/角色管理")
    public void addRoleClass(@RequestBody MayanPlatRoleClassVo roleClassVo){
        roleService.addRoleClass(roleClassVo);
    }


    /**
     * 修改角色类别
     */
    @ApiOperation(value = "修改角色类别")
    @PostMapping(value = "/updateRoleClass")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "修改角色类别", catalog = "权限管理/角色管理")
    public void updateRoleClass(@RequestBody MayanPlatRoleClassVo roleClassVo){
        roleService.updateRoleClass(roleClassVo);
    }

    /**
     * 删除角色类别
     */
    @ApiOperation(value = "删除角色类别")
    @PostMapping(value = "/deleteRoleClass")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "删除角色类别", catalog = "权限管理/角色管理")
    public void deleteRoleClass(@RequestBody Map<String,String> map){
        String id = map.get("id");
        roleService.deleteRoleClass(id);
    }


    /**
     * 查询所有角色类别
     */
    @ApiOperation(value = "查询所有角色类别")
    @GetMapping(value = "/getClassList")
    public List<MayanPlatRoleClassVo> getList(){
        return roleService.getList();
    }


    @ApiOperation(value = "查询角色列表")
    @PostMapping(value = "/getRoleList")
    public PageVo<MayanPlatRole> getRoleList(@RequestBody RolePageRequest pageRequest){
        return roleService.getRoleList(pageRequest);
    }



    /**
     * 添加角色
     */
    @ApiOperation(value = "添加角色")
    @PostMapping(value = "/addRole")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "添加角色", catalog = "权限管理/角色管理")
    public void addRole(@RequestBody MayanPlatRoleVo roleVo){
        roleService.addRole(roleVo);
    }

    /**
     * 更新角色
     */
    @ApiOperation(value = "更新角色")
    @PostMapping(value = "/updateRole")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "更新角色", catalog = "权限管理/角色管理")
    public void updateRole(@RequestBody MayanPlatRoleVo roleVo){
        roleService.updateRole(roleVo);
    }

    /**
     * 删除角色
     */
    @ApiOperation(value = "删除角色")
    @PostMapping(value = "/removeRole")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "删除角色", catalog = "权限管理/角色管理")
    public void removeRole(@RequestBody Map<String,String> map){
        roleService.removeRole(map.get("id"));
    }


    /**
     * 查询角色关联的用户列表
     */
    @ApiOperation(value = "查询角色关联的用户列表")
    @PostMapping(value = "/getUserListByRoleCode")
    public PageVo<MayanPlatUserVo> getUserListByRoleCode(@RequestBody RoleUserPageRequest pageRequest){
        return roleService.getUserListByRoleCode(pageRequest);
    }


    /**
     * 角色关联用户
     */
    @ApiOperation(value = "角色关联用户")
    @PostMapping(value = "/relateUser")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "角色关联用户", catalog = "权限管理/角色管理")
    public void relateUser(@RequestBody MayanPlatRoleUserVo userVo){
        roleService.relateUser(userVo);
    }

    /**
     * 角色关联用户
     */
    @ApiOperation(value = "角色关联用户")
    @PostMapping(value = "/relateUserList")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "角色关联用户", catalog = "权限管理/角色管理")
    public void relateUserList(@RequestBody List<MayanPlatRoleUserVo> userVos){
        roleService.relateUserList(userVos);
    }


    /**
     * 删除角色关联用户 删除关联关系
     */
    @ApiOperation(value = "删除角色关联用户 删除关联关系")
    @PostMapping(value = "/removeRelateUser")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "删除角色关联用户 删除关联关系", catalog = "权限管理/角色管理")
    public void removeRelateUser(@RequestBody Map<String,String> map){
        roleService.removeRelateUser(map.get("relateId"));
    }

    /**
     * 删除角色关联用户 删除关联关系
     */
    @ApiOperation(value = "删除角色关联用户 删除关联关系")
    @PostMapping(value = "/removeRelateUserList")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "删除角色关联用户 删除关联关系", catalog = "权限管理/角色管理")
    public void removeRelateUserList(@RequestBody List<String> relateIds){
        roleService.removeRelateUserList(relateIds);
    }


    /**
     * 通过角色查询关联资源
     */
    @ApiOperation(value = "通过角色查询关联资源")
    @GetMapping(value = "/queryRelateResource")
    public RoleResourceEntity queryRelateResource(@RequestParam(value = "roleCode") String roleCode){
        return roleService.queryRelateResource(roleCode);
    }



    /**
     * 保存全部关联资源  全删全增
     */
    @ApiOperation(value = "保存全部关联资源  全删全增")
    @PostMapping(value = "/relateResource")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "保存全部关联资源  全删全增", catalog = "权限管理/角色管理")
    public void relateResource(@RequestBody ResourceRoleVo resourceRoleVo){
        roleService.relateResource(resourceRoleVo);
    }


    /**
     * 保存角色数据权限配置
     */
    @ApiOperation(value = "保存角色数据权限配置")
    @PostMapping(value = "/saveRoleDataConfig")
    @LogSaveAnnotation(module = PlatConstant.UC_APP_MODULE, recordRequestParam = true, title = "保存角色数据权限配置", catalog = "权限管理/角色管理")
    public void saveRoleDataConfig(@RequestBody MayanPlatDataRoleConfigVo roleConfigVo){
        roleService.saveRoleDataConfig(roleConfigVo);
    }


    /**
     * 查询角色数据权限
     */
    @ApiOperation(value = "查询角色数据权限")
    @GetMapping(value = "/queryDataRoleConfig")
    public MayanPlatDataRoleConfigVo queryDataRoleConfig(String roleCode){
        return roleService.queryDataRoleConfig(roleCode);
    }

}
